var betterga_preferencesService = null;

// Deletes a preference
function betterga_deletePreference( preference ){
	// If the preference is set
	if( preference ){
		// If a user preference is set
		if( betterga_isPreferenceSet( preference ) ){
			betterga_getPreferencesService().clearUserPref( preference );
		}
	}
}

// Deletes a preference branch
function betterga_deletePreferenceBranch( branch ){
	// If the branch is set
	if( branch ){
		betterga_getPreferencesService().deleteBranch( branch );
	}
}

// Gets a boolean preference, returning false if the preference is not set
function betterga_getBooleanPreference( preference, userPreference ){
	// If the preference is set
	if( preference ){
		// If not a user preference or a user preference is set
		if( !userPreference || betterga_isPreferenceSet( preference ) ){
			try{
				return betterga_getPreferencesService().getBoolPref( preference );
			}
			catch( exception ){
				// Do nothing
			}
		}
	}

	return false;
}

// Gets an integer preference, returning 0 if the preference is not set
function betterga_getIntegerPreference( preference, userPreference ){
	// If the preference is set
	if( preference ) {
		// If not a user preference or a user preference is set
		if( !userPreference || betterga_isPreferenceSet( preference ) ){
			try{
				return betterga_getPreferencesService().getIntPref( preference );
			}
			catch( exception ){
				// Do nothing
			}
		}
	}

	return 0;
}

// Gets the preferences service
function betterga_getPreferencesService(){
	// If the preferences service is not set
	if( !betterga_preferencesService ){
		betterga_preferencesService = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService).getBranch("");
	}

	return betterga_preferencesService;
}

// Gets a string preference, returning null if the preference is not set
function betterga_getStringPreference(preference, userPreference){
	// If the preference is set
	if(preference){
		// If not a user preference or a user preference is set
		if( !userPreference || betterga_isPreferenceSet( preference ) ){
			try{
				return betterga_getPreferencesService().getComplexValue( preference, Components.interfaces.nsISupportsString ).data;
			}
			catch( exception ){
				// Do nothing
				//alert(exception);
			}
		}
	}

	return null;
}

// Is a preference set
function betterga_isPreferenceSet( preference ){
	// If the preference is set
	if( preference ) {
		return betterga_getPreferencesService().prefHasUserValue( preference );
	}

	return false;
}

// Sets a boolean preference
function betterga_setBooleanPreference(preference, value){
	// If the preference is set
	if(preference) {
		betterga_getPreferencesService().setBoolPref(preference, value);
	}
}

// Sets a boolean preference if it is not already set
function betterga_setBooleanPreferenceIfNotSet(preference, value){
	// If the preference is not set
	if(!betterga_isPreferenceSet(preference)){
		betterga_getPreferencesService().setBoolPref(preference, value);
	}
}

// Sets an integer preference
function betterga_setIntegerPreference(preference, value){
	// If the preference is set
	if(preference) {
		betterga_getPreferencesService().setIntPref(preference, value);
	}
}

// Sets an integer preference if it is not already set
function betterga_setIntegerPreferenceIfNotSet(preference, value){
	// If the preference is not set
	if(!betterga_isPreferenceSet(preference)){
		betterga_setIntegerPreference(preference, value);
	}
}

// Sets a string preference
function betterga_setStringPreference(preference, value){
	// If the preference is set
	if(preference) {
		var supportsStringInterface	= Components.interfaces.nsISupportsString;
		var string					= Components.classes["@mozilla.org/supports-string;1"].createInstance(supportsStringInterface);

		string.data = value;

		betterga_getPreferencesService().setComplexValue(preference, supportsStringInterface, string);
	}
}

// Sets a string preference if it is not already set
function betterga_setStringPreferenceIfNotSet(preference, value){
	// If the preference is not set
	if(!betterga_isPreferenceSet(preference)){
		betterga_setStringPreference(preference, value);
	}
}

function betterga_setStringPreferenceWithForce(preference, value, force){
	if(force){
		betterga_setStringPreference(preference, value);
	}
	else{
		betterga_setStringPreferenceIfNotSet(preference, value);
	}
}

function betterga_setIntegerPreferenceWithForce(preference, value, force){
	if(force){
		betterga_setIntegerPreference(preference, value);
	}
	else{
		betterga_setIntegerPreferenceIfNotSet(preference, value);
	}
}

function betterga_setBooleanPreferenceWithForce(preference, value, force){
	if(force){
		betterga_setBooleanPreference(preference, value);
	}
	else{
		betterga_setBooleanPreferenceIfNotSet(preference, value);
	}
}